package com.dcy.znoa.common.util.wj;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;

/**
 * wj 八元二次方程组 求最优解
 * v1.0 目前只有自然数最优解
 * Created by pengyu on 2018/06/01.
 */
public class HouseWJ1 {


    public static void main(String[] args) {
        /*
        不带底商公式：
        420X1Y1+400X2Y2+400X3Y3+270X4Y4+440X5Y5+150X6Y6+1*1*3200 ≤ 171060；——此为计容面积约束 167860
        420*Y1+400*Y2+400Y3+270Y4 +440Y5+150Y6+3200 ≤9706；——此为基地面积约束
        最低层数 ≤ Xi ≤最高层数，i=1,2,3,4,5,6；——此为层数限制
        Xi ≥ 0，i=1,2,3,4,5,6。——此为理论模型的要求
        ※求出来的值若不为整数，请只保留小数点后2位
        * */

        /*
        带底商公式：
        420X1Y1+400X2Y2+400X3Y3+270X4Y4+440X5Y5+150X6Y6+1*1*3200 ≤ 171060；——此为计容面积约束 167860
        420/2*Y1+0*Y2+400Y3+270Y4 +440Y5+150Y6+3200 ≤9706；——此为基地面积约束
        最低层数 ≤ Xi ≤最高层数，i=1,2,3,4,5,6；——此为层数限制
        Xi ≥ 0，i=1,2,3,4,5,6。——此为理论模型的要求
        ※求出来的值若不为整数，请只保留小数点后2位
        * */

//        //不带底商
//        DongCengInput dongCengInput1 =  new DongCengInput(420, 400, 270, 150,
//                420, 400, 270, 150,
//                18, 33, 11, 18, 11, 18, 1, 4,
//                7000, 7500, 8000, 10000, 171060, 9706,0);
//        getDongCeng(dongCengInput1);
        //带底商
//        DongCengInput dongCengInput2 =  new DongCengInput(420, 400, 270, 150,
//                420/2, 400*0, 270, 150,
//                18, 33, 11, 18, 11, 18, 1, 4,
//                7000, 7500, 8000, 10000, 171060 -1*1*3200, 9706 - 3200,1*1*3200*18000);
//        getDongCeng(dongCengInput2);
        //180713
        //不带底商

//        DongCengInput dongCengInput1 =  new DongCengInput(500, 450, 270, 150,
//                500, 450, 270, 150,
//                18, 33, 11, 18, 11, 18, 1, 4,
//                7000, 7500, 8000, 12000, 171060, 9706,0);
//        getDongCeng(dongCengInput1);

//        DongCengInput dongCengInput1 =  new DongCengInput(400, 450, 300, 300,
//                400, 450, 300, 300,
//                18, 33, 11, 18, 11, 18, 1, 4,
//                7000, 8000, 7500, 20000, 171060, 9706,0);
//        getDongCeng(dongCengInput1);

//        DongCengInput dongCengInput1 =  new DongCengInput(400, 450, 450, 600,
//                400, 450, 450, 600,
//                11, 11, 33, 33, 18, 18, 2, 4,
//                13000, 12000, 11500, 15000, 171060, 9706,0);
//        getDongCeng(dongCengInput1);

        DongCengInput dongCengInput1 =  new DongCengInput(400, 450, 450, 600,
                400, 450, 450, 600,
                9, 11, 33, 33, 11, 18, 2, 4,
                13000, 12000, 11500, 20000, 171060, 9706,0);
        getDongCeng(dongCengInput1);

/*
* startTime = Fri Jul 13 09:50:53 CST 2018
xyMinAcreage = 150
yMinBaseArea = 150
y1Max = 20
y2Max = 22
y3Max = 36
y4Max = 65
循环次数为：1000000000
循环次数为：2000000000
循环次数为：3000000000
循环次数为：4000000000
x1R=18 y1R=0 x2R=17 y2R=2 x3R=18 y3R=32 x4R=1 y4R=1 stR=9690 btR=170970 result=1360710000
x1R=18 y1R=0 x2R=11 y2R=0 x3R=18 y3R=35 x4R=4 y4R=1 stR=9600 btR=170700 result=1368000000
* */

//        DongCengInput dongCengInput1 = new DongCengInput(500, 450, 270, 150,
//                500, 450, 270, 150,
//                18, 33, 11, 18, 11, 18, 1, 4,
//                0, 0, 8000, 12000, 171060, 9706, 0);
//        getDongCeng34(dongCengInput1);

    }


    public static void writeToFile2(String content) {
        try {
//            String content = "测试使用字符串";
            File file = new File("d:/fangdichan.txt");
            //文件不存在时候，主动创建文件。
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file, true);//true为追加
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            fw.close();
            System.out.println("test2 done!");

        } catch (Exception e) {
        }
    }

    // 8个未知数 未做数据校验，请不要输入负数或大大超过各范围的参数
    public static DongCengResult getDongCeng(DongCengInput dongCengInput) {
//        public static List<DongCeng> getDongCengList0627_1_c(int x1y1, int x2y2, int x3y3, int x4y4, int bigTotal, int baseAreaInput) {
        System.out.println("startTime = " + new Date());
        int x1y1Acreage = dongCengInput.getX1y1Acreage();
        int x2y2Acreage = dongCengInput.getX2y2Acreage();
        int x3y3Acreage = dongCengInput.getX3y3Acreage();
        int x4y4Acreage = dongCengInput.getX4y4Acreage();
        int xyMinAcreage = dongCengInput.getXyMinAcreage();
        System.out.println("xyMinAcreage = " + xyMinAcreage);
        int y1BaseArea = dongCengInput.getY1BaseArea();
        int y2BaseArea = dongCengInput.getY2BaseArea();
        int y3BaseArea = dongCengInput.getY3BaseArea();
        int y4BaseArea = dongCengInput.getY4BaseArea();
        int yMinBaseArea = dongCengInput.getYMinBaseArea();
        System.out.println("yMinBaseArea = " + yMinBaseArea);
        int x1Max = dongCengInput.getX1Max();
        int x1Min = dongCengInput.getX1Min();
        int x2Max = dongCengInput.getX2Max();
        int x2Min = dongCengInput.getX2Min();
        int x3Max = dongCengInput.getX3Max();
        int x3Min = dongCengInput.getX3Min();
        int x4Max = dongCengInput.getX4Max();
        int x4Min = dongCengInput.getX4Min();
        int temp = dongCengInput.getTemp();
        int acreageInput = dongCengInput.getAcreageInput();
        int baseAreaInput = dongCengInput.getBaseAreaInput();
        int y1Max = baseAreaInput % x1y1Acreage == 0 ? baseAreaInput / x1y1Acreage : baseAreaInput / x1y1Acreage + 1;
        int y2Max = 0;
        if (y2BaseArea == 0) {
            y2Max = 100;
        } else {
            y2Max = baseAreaInput % x2y2Acreage == 0 ? baseAreaInput / x2y2Acreage : baseAreaInput / x2y2Acreage + 1;
        }
        int y3Max = baseAreaInput % x3y3Acreage == 0 ? baseAreaInput / x3y3Acreage : baseAreaInput / x3y3Acreage + 1;
        int y4Max = baseAreaInput % x4y4Acreage == 0 ? baseAreaInput / x4y4Acreage : baseAreaInput / x4y4Acreage + 1;
        //栋数均不超过100
        if (y1Max > 100) {
            y1Max = 100;
        }
        if (y2Max > 100) {
            y2Max = 100;
        }
        if (y3Max > 100) {
            y3Max = 100;
        }
        if (y4Max > 100) {
            y4Max = 100;
        }
        System.out.println("y1Max = " + y1Max);
        System.out.println("y2Max = " + y2Max);
        System.out.println("y3Max = " + y3Max);
        System.out.println("y4Max = " + y4Max);
        int minY = 0;
        long countNum = 0;
        int countTotal = 0;
        int result = 0;
        int x1R = 0, x2R = 0, x3R = 0, x4R = 0, y1R = 0, y2R = 0, y3R = 0, y4R = 0;
        int stR = 0, btR = 0;
        DongCengResult dcr = new DongCengResult();
        for (int x1No = x1Min; x1No <= x1Max; x1No++) {//xMax = 10
            for (int x2No = x2Min; x2No <= x2Max; x2No++) {
                for (int x3No = x3Min; x3No <= x3Max; x3No++) {
                    for (int x4No = x4Min; x4No <= x4Max; x4No++) {
                        for (int y1No = minY; y1No <= y1Max; y1No++) {
                            for (int y2No = minY; y2No <= y2Max; y2No++) {
                                for (int y3No = minY; y3No <= y3Max; y3No++) {
                                    for (int y4No = minY; y4No <= y4Max; y4No++) {
                                        countNum = countNum + 1L;
                                        if (countNum % 1000000000 == 0) {
                                            System.out.println("循环次数为：" + countNum);
                                        }
//                                        int st = x1y1 * y1No + x2y2 * y2No + x3y3 * y3No + x4y4 * y4No;
//                                        int bt = x1y1 * y1No * x1No + x2y2 * y2No * x2No + x3y3 * y3No * x3No + x4y4 * y4No * x4No;
//                                        dcr = new DongCengResult(x1No,x2No,x3No,x4No,y1No,y2No,y3No,y4No);
                                        dcr.init(x1No, x2No, x3No, x4No, y1No, y2No, y3No, y4No);
                                        int tempAcreage = dcr.getTempAcreage(x1y1Acreage, x2y2Acreage, x3y3Acreage, x4y4Acreage);
                                        int tempBaseArea = dcr.getTempBaseArea(y1BaseArea, y2BaseArea, y3BaseArea, y4BaseArea);
//                                        if ((tempBaseArea <= baseAreaInput) && (tempBaseArea >= (baseAreaInput - yMinBaseArea)) &&
//                                                (tempAcreage <= acreageInput) && (tempAcreage >= (acreageInput - xyMinAcreage))) {
                                        if ((tempBaseArea <= baseAreaInput) && (tempBaseArea >= (baseAreaInput - 3000)) &&
                                                (tempAcreage <= acreageInput) && (tempAcreage >= (acreageInput - 30000))) {
                                            countTotal++;
//                                            int resultTemp = x1No * y1No * 420 * 7000 + x2No * y2No * 400 * 7500 + x3No * y3No * 270 * 8000 + x4No * y4No * 150 * 10000 ;
                                            int resultTemp = dongCengInput.getValue(x1No, y1No, x2No, y2No, x3No, y3No, x4No, y4No, temp);
                                            if (resultTemp > result) {
                                                result = resultTemp;
                                                x1R = x1No;
                                                x2R = x2No;
                                                x3R = x3No;
                                                x4R = x4No;
                                                y1R = y1No;
                                                y2R = y2No;
                                                y3R = y3No;
                                                y4R = y4No;
                                                stR = tempBaseArea;
                                                btR = tempAcreage;
                                            }
                                        }
                                        dcr.destory();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
//        writeToFile2("x1R=" + x1R + " y1R=" + y1R +
//                " x2R=" + x2R + " y2R=" + y2R +
//                " x3R=" + x3R + " y3R=" + y3R +
//                " x4R=" + x4R + " y4R=" + y4R +
//                " stR=" + stR + " btR=" + btR +
//                " result=" + result + "\r\n");
        System.out.println("x1R=" + x1R + " y1R=" + y1R +
                " x2R=" + x2R + " y2R=" + y2R +
                " x3R=" + x3R + " y3R=" + y3R +
                " x4R=" + x4R + " y4R=" + y4R +
                " stR=" + stR + " btR=" + btR +
                " result=" + result + "\r\n");
        System.out.println("总次数 = " + countTotal);
        System.out.println("endTime = " + new Date());
//        writeToFile2("总次数 = " + countTotal + "\r\n");
//        writeToFile2("endTime = " + new Date() + "\r\n");
        DongCengResult dongCengResult = new DongCengResult(x1R, x2R, x3R, x4R, y1R, y2R, y3R, y4R, btR, stR, result);

        int z4 = dongCengInput.getZ4();
        System.out.println("z4 = " + z4);
        System.out.println("acreageInput = " + acreageInput);
        System.out.println("btR = " + btR);
        System.out.println("y4BaseArea = " + y4BaseArea);
        System.out.println("x4R = " + x4R);
        System.out.println("baseAreaInput = " + baseAreaInput);
        System.out.println("stR = " + stR);

//        float a1 = (acreageInput -  btR) / (y4BaseArea * x4R);
//        float b1 = (baseAreaInput -  stR) / y4BaseArea ;
//        float end = a1<b1?a1:b1;
//        result = (int) (result + z4 * end * y4BaseArea);
//        System.out.println("a1 = " + a1);
//        System.out.println("b1 = " + b1);
//        System.out.println("end = " + end);

        //        float as = 106/160;
//        BigDecimal a = new BigDecimal(106);
//        BigDecimal b = new BigDecimal(160);
//        BigDecimal bs = a.divide(b, 2, RoundingMode.HALF_UP);
//        System.out.println(bs);

//        0.1 与 1 之间
        BigDecimal a2  = new BigDecimal(acreageInput -  btR);
        BigDecimal a3  = new BigDecimal(y4BaseArea * x4R);
        BigDecimal a1 = a2.divide(a3, 2, RoundingMode.HALF_UP);

        BigDecimal b2  = new BigDecimal(baseAreaInput -  stR);
        BigDecimal b3  = new BigDecimal(y4BaseArea);
        BigDecimal b1 = b2.divide(b3, 2, RoundingMode.HALF_UP);

        BigDecimal resultBd = a1.compareTo(b1)<0?a1:b1;
        System.out.println("a1 = " + a1);
        System.out.println("b1 = " + b1);
        System.out.println("resultBd = " + resultBd);
        BigDecimal resultBdEnd = resultBd.multiply(new BigDecimal(z4 * y4BaseArea * x4R)).add(new BigDecimal(result));

        System.out.println("resultBdEnd = " + resultBdEnd);
//        System.out.println("result = " + result);
        return dongCengResult;
    }


    // 只计算3 4
    public static DongCengResult getDongCeng34(DongCengInput dongCengInput) {
//        public static List<DongCeng> getDongCengList0627_1_c(int x1y1, int x2y2, int x3y3, int x4y4, int bigTotal, int baseAreaInput) {
        System.out.println("startTime = " + new Date());
        int x1y1Acreage = dongCengInput.getX1y1Acreage();
        int x2y2Acreage = dongCengInput.getX2y2Acreage();
        int x3y3Acreage = dongCengInput.getX3y3Acreage();
        int x4y4Acreage = dongCengInput.getX4y4Acreage();
        int xyMinAcreage = dongCengInput.getXyMinAcreage();
        System.out.println("xyMinAcreage = " + xyMinAcreage);
        int y1BaseArea = dongCengInput.getY1BaseArea();
        int y2BaseArea = dongCengInput.getY2BaseArea();
        int y3BaseArea = dongCengInput.getY3BaseArea();
        int y4BaseArea = dongCengInput.getY4BaseArea();
        int yMinBaseArea = dongCengInput.getYMinBaseArea();
        System.out.println("yMinBaseArea = " + yMinBaseArea);
        int x1Max = dongCengInput.getX1Max();
        int x1Min = dongCengInput.getX1Min();
        int x2Max = dongCengInput.getX2Max();
        int x2Min = dongCengInput.getX2Min();
        int x3Max = dongCengInput.getX3Max();
        int x3Min = dongCengInput.getX3Min();
        int x4Max = dongCengInput.getX4Max();
        int x4Min = dongCengInput.getX4Min();
        int temp = dongCengInput.getTemp();
        int acreageInput = dongCengInput.getAcreageInput();
        int baseAreaInput = dongCengInput.getBaseAreaInput();
        int y1Max = baseAreaInput % x1y1Acreage == 0 ? baseAreaInput / x1y1Acreage : baseAreaInput / x1y1Acreage + 1;
        int y2Max = baseAreaInput % x2y2Acreage == 0 ? baseAreaInput / x2y2Acreage : baseAreaInput / x2y2Acreage + 1;;
//        if (y2BaseArea == 0) {
//            y2Max = 100;
//        } else {
//            y2Max = baseAreaInput % x2y2Acreage == 0 ? baseAreaInput / x2y2Acreage : baseAreaInput / x2y2Acreage + 1;
//        }
        int y3Max = baseAreaInput % x3y3Acreage == 0 ? baseAreaInput / x3y3Acreage : baseAreaInput / x3y3Acreage + 1;
        int y4Max = baseAreaInput % x4y4Acreage == 0 ? baseAreaInput / x4y4Acreage : baseAreaInput / x4y4Acreage + 1;
        //栋数均不超过100
        if (y1Max > 100) {
            y1Max = 100;
        }
        if (y2Max > 100) {
            y2Max = 100;
        }
        if (y3Max > 100) {
            y3Max = 100;
        }
        if (y4Max > 100) {
            y4Max = 100;
        }
        System.out.println("y1Max = " + y1Max);
        System.out.println("y2Max = " + y2Max);
        System.out.println("y3Max = " + y3Max);
        System.out.println("y4Max = " + y4Max);
        int minY = 0;
        long countNum = 0;
        int countTotal = 0;
        int result = 0;
        int x1R = 0, x2R = 0, x3R = 0, x4R = 0, y1R = 0, y2R = 0, y3R = 0, y4R = 0;
        int stR = 0, btR = 0;
        DongCengResult dcr = new DongCengResult();
        for (int x1No = x1Min; x1No <= x1Max; x1No++) {//xMax = 10
            for (int x2No = x2Min; x2No <= x2Max; x2No++) {
                for (int x3No = x3Min; x3No <= x3Max; x3No++) {
                    for (int x4No = x4Min; x4No <= x4Max; x4No++) {
                        for (int y1No = minY; y1No <= y1Max; y1No++) {
                            for (int y2No = minY; y2No <= y2Max; y2No++) {
                                for (int y3No = minY; y3No <= y3Max; y3No++) {
                                    for (int y4No = minY; y4No <= y4Max; y4No++) {
                                        countNum = countNum + 1L;
                                        if (countNum % 1000000000 == 0) {
                                            System.out.println("循环次数为：" + countNum);
                                        }
//                                        int st = x1y1 * y1No + x2y2 * y2No + x3y3 * y3No + x4y4 * y4No;
//                                        int bt = x1y1 * y1No * x1No + x2y2 * y2No * x2No + x3y3 * y3No * x3No + x4y4 * y4No * x4No;
//                                        dcr = new DongCengResult(x1No,x2No,x3No,x4No,y1No,y2No,y3No,y4No);
                                        dcr.init(x1No, x2No, x3No, x4No, y1No, y2No, y3No, y4No);
                                        int tempAcreage = dcr.getTempAcreage(x1y1Acreage, x2y2Acreage, x3y3Acreage, x4y4Acreage);
                                        int tempBaseArea = dcr.getTempBaseArea(y1BaseArea, y2BaseArea, y3BaseArea, y4BaseArea);
                                        if ((tempBaseArea <= baseAreaInput) && (tempBaseArea >= (baseAreaInput - 500)) &&
                                                (tempAcreage <= acreageInput) && (tempAcreage >= (acreageInput - 500))) {
                                            countTotal++;
//                                            int resultTemp = x1No * y1No * 420 * 7000 + x2No * y2No * 400 * 7500 + x3No * y3No * 270 * 8000 + x4No * y4No * 150 * 10000 ;
                                            int resultTemp = dongCengInput.getValue(x1No, y1No, x2No, y2No, x3No, y3No, x4No, y4No, temp);
                                            if (resultTemp > result) {
                                                result = resultTemp;
                                                x1R = x1No;
                                                x2R = x2No;
                                                x3R = x3No;
                                                x4R = x4No;
                                                y1R = y1No;
                                                y2R = y2No;
                                                y3R = y3No;
                                                y4R = y4No;
                                                stR = tempBaseArea;
                                                btR = tempAcreage;
                                            }
                                        }
                                        dcr.destory();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
//        writeToFile2("x1R=" + x1R + " y1R=" + y1R +
//                " x2R=" + x2R + " y2R=" + y2R +
//                " x3R=" + x3R + " y3R=" + y3R +
//                " x4R=" + x4R + " y4R=" + y4R +
//                " stR=" + stR + " btR=" + btR +
//                " result=" + result + "\r\n");
        System.out.println("x1R=" + x1R + " y1R=" + y1R +
                " x2R=" + x2R + " y2R=" + y2R +
                " x3R=" + x3R + " y3R=" + y3R +
                " x4R=" + x4R + " y4R=" + y4R +
                " stR=" + stR + " btR=" + btR +
                " result=" + result + "\r\n");
        System.out.println("总次数 = " + countTotal);
        System.out.println("endTime = " + new Date());
//        writeToFile2("总次数 = " + countTotal + "\r\n");
//        writeToFile2("endTime = " + new Date() + "\r\n");
        DongCengResult dongCengResult = new DongCengResult(x1R, x2R, x3R, x4R, y1R, y2R, y3R, y4R, btR, stR, result);
        return dongCengResult;
    }



}
